package com.dearbinge.opentapi.content;

import com.dearbinge.data.redis.service.RedisService;
import com.dearbinge.utils.ResultMsg;
import com.dearbinge.utils.sms.demo.SmsSendDemo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Api(description = "发送短信")
@RestController
@CrossOrigin
@RequestMapping("/sms")
@EnableAutoConfiguration
public class BaseSms extends WebMvcConfigurerAdapter {

    @Autowired
    private RedisService redisService;


    public String smsCode(){
        int code = (int)((Math.random()*9+1)*100000);
        return String.valueOf(code);
    }

    @ApiOperation(value = "发送验证码",notes = "发送验证码")
    @ApiImplicitParam(name = "phone",value = "手机号码",paramType = "query")
    @RequestMapping(value = "/sendSms",method = RequestMethod.GET)
    public ResultMsg<String> sendSms(String phone){
        try {
            String smsCode = smsCode();
            String sms = SmsSendDemo.sendSms(smsCode, phone);
            if (sms == "true"){
                redisService.set(phone+"smsCode",smsCode);
                //设置两分钟过期
                redisService.expired(phone+"smsCode", 900);
                return ResultMsg.ok(sms);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

}
